package com.caishi.lkx.order.pay;

import com.caishi.lkx.order.config.AssetsRouter;
import com.caishi.lkx.order.ienum.type.PaymentType;
import com.caishi.lkx.order.model.OrderDetailModel;
import com.caishi.lkx.order.model.OrderModel;
import com.caishi.lkx.order.model.PaymentModel;
import com.caishi.lkx.order.model.PaymentRefundLogModel;
import com.caishi.lkx.order.service.IOrderDetailService;
import com.caishi.lkx.order.service.IOrderService;
import com.caishi.lkx.order.service.IUserAssetsDataService;
import com.caishi.lkx.user.service.IAgentUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Param:
 * @return
 * @Author: huangpeng
 * @Date: 2021/11/24 10:58
 * @Description:
 */
@Configuration
@Slf4j
public class AgentPay implements PayPlugins {
    @Resource
    private IAgentUserService agentUserService;
    @Resource
    private IOrderService orderService;
    @Resource
    private IOrderDetailService orderDetailService;
    @Resource
    private IUserAssetsDataService userAssetsDataService;

    @Override
    public Map<String, Object> sign(PaymentModel paymentModel, Object val, Map<String, Object> otherParam) {
        return new HashMap<>();
    }


    @Override
    public boolean refund(PaymentModel paymentModel, PaymentRefundLogModel paymentRefundLogModel)   {
        paymentRefundLogModel.setRefundType(PaymentType.agentpay);
        OrderModel order = orderService.getById(paymentModel.getOrderId());
            List<OrderDetailModel> orderDetailModels = orderDetailService.selectOrderDetails(order.getId());
            orderDetailModels.forEach(d -> {
                AssetsRouter.router.router(d.getEntityType()).assetsRefundCall(order, userAssetsDataService.getById(d.getUserAssetId()));
            });
        agentUserService.refuse(paymentModel.getPayUserId(), order.getPrice(),order.getId());
        return true;

    }

    @Override
    public void postPayCallBack(PaymentModel paymentModel) {
        agentUserService.cosumer(paymentModel.getPayUserId(), paymentModel.getPaymentAmount(),paymentModel.getOrderId());
    }
}
